var lineTable;
var stocksTable;

function checkAttributes() {
	disabledFields();
	if ($("[id='purchase_order_form:po_number']").val() == '') {
		$("[id='purchase_order_form:complete_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:finalise_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:cancel_btn']").addClass("disable_btn");
	}
	else
	{
		$("[id='purchase_order_form:transaction_code_btn']").attr("disabled", "true");
	}

	if ($("[id='purchase_order_form:store_code_string']").val() != '') {
		$("[id='purchase_order_form:store_code_btn']").attr("disabled", "true");
		$("[id='purchase_order_form:po_number_btn']").attr("disabled", "true");
	}

	if ($("[id='purchase_order_form:store_code']").val() != '') {
		$("[id='purchase_order_form:item_code_btn']").removeAttr("disabled");
	} else {
		$("[id='purchase_order_form:item_code_btn']").attr("disabled", "true");
	}

	if ($("#po_lines_table_body tr").length > 0) {
		$("[id='purchase_order_form:store_code_btn']").attr("disabled", "true");
	}

	if ($("[id='purchase_order_form:status']").val() == 'Entering') {
		$("[id='purchase_order_form:finalise_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:complete_btn']").removeClass("disable_btn");
		$("[id='purchase_order_form:cancel_btn']").removeClass("disable_btn");
		
		
		
	}
	if ($("[id='purchase_order_form:status']").val() == 'Completed') {
		$("[id='purchase_order_form:cancel_btn']").removeClass("disable_btn");
		$("[id='purchase_order_form:complete_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:finalise_btn']").removeClass("disable_btn");

		// fields
		$("[id='purchase_order_form:postage']").attr("disabled", "true");
		$("[id='purchase_order_form:buyer']").attr("disabled", "true");
		$("[id='purchase_order_form:delivery_address']").attr("disabled",
				"true");
		$(".price_column").attr("disabled", "true");
		$(".qty_column").attr("disabled", "true");
		$(".delete_column").attr("disabled", "true");
		$("[id='purchase_order_form:item_code_btn']").attr("disabled", "true");
	}
	if ($("[id='purchase_order_form:status']").val() == 'Cancelled') {
		$("[id='purchase_order_form:save_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:complete_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:finalise_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:cancel_btn']").addClass("disable_btn");

		//disabledFields();
		
		// fields
		
		$("[id='purchase_order_form:postage']").attr("disabled", "true");
		$("[id='purchase_order_form:buyer']").attr("disabled", "true");
		$("[id='purchase_order_form:delivery_address']").attr("disabled",
				"true");
		$("[id='purchase_order_form:comments']").attr("disabled", "true");
		$(".price_column").attr("disabled", "true");
		$(".qty_column").attr("disabled", "true");
		$(".delete_column").attr("disabled", "true");
		$("[id='purchase_order_form:item_code_btn']").attr("disabled", "true");
		$("[id='purchase_order_form:officer_code_btn']").attr("disabled", "true");
	}
	if ($("[id='purchase_order_form:status']").val() == 'Finalised') {
		$("[id='purchase_order_form:save_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:complete_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:finalise_btn']").addClass("disable_btn");
		$("[id='purchase_order_form:cancel_btn']").addClass("disable_btn");

		//disabledFields();
		// fields
		
		$("[id='purchase_order_form:postage']").attr("disabled", "true");
		$("[id='purchase_order_form:buyer']").attr("disabled", "true");
		$("[id='purchase_order_form:delivery_address']").attr("disabled",
				"true");
		$("[id='purchase_order_form:comments']").attr("disabled", "true");
		$(".price_column").attr("disabled", "true");
		$(".qty_column").attr("disabled", "true");
		$(".delete_column").attr("disabled", "true");
		$("[id='purchase_order_form:item_code_btn']").attr("disabled", "true");
		$("[id='purchase_order_form:officer_code_btn']").attr("disabled", "true");
	}
	
}

function disabledFields() {
	$("[id='purchase_order_form:po_number']").attr("disabled", "true");
	$("[id='purchase_order_form:transaction_code']").attr("disabled", "true");
	$("[id='purchase_order_form:officer_code']").attr("disabled", "true");
	$("[id='purchase_order_form:store_code']").attr("disabled", "true");
	$("[id='purchase_order_form:transaction_date']").attr("disabled", "true");
	$("[id='purchase_order_form:status']").attr("disabled", "true");
	$(".item_code_column").attr("disabled", "true");
	$("[id='purchase_order_form:complete_btn']").addClass("disable_btn");
	$("[id='purchase_order_form:finalise_btn']").addClass("disable_btn");
	$("[id='purchase_order_form:cancel_btn']").addClass("disable_btn");
}

function validatePO() {
	var transactionCode = $("[id='purchase_order_form:transaction_code']")
			.val();
	var buyer = $("[id='purchase_order_form:buyer']").val();
	var deliveryAdd = $("[id='purchase_order_form:delivery_address']").val();
	var officer_code = $("[id='purchase_order_form:officer_code']").val();
	if (transactionCode.trim().length <= 0 || officer_code.trim().length <= 0 || buyer.trim().length <= 0
			|| deliveryAdd.trim().length <= 0) {
		alert("Transaction Code, Buyer and Delivery Address fields cannot be empty!");
		return false;
	} else {
		return true;
	}
}

function saveBtnOnclick()
{
	$("[id='purchase_order_form:delivery_address_hidden']").val($("[id='purchase_order_form:delivery_address']").val());
	$("[id='purchase_order_form:postage_hidden']").val($("[id='purchase_order_form:postage']").val());
	$("[id='purchase_order_form:buyer_hidden']").val($("[id='purchase_order_form:buyer']").val());
	return validateLines();

}


function validateLines() {
	if (!validatePO()) {
		return false;
	}
	var flag = true;

	var length = $('#po_lines_table_body tr').length;
	var rows = linesTable.fnGetNodes();
	for ( var index = 0; index < length; index++) {

		var price = $(rows[index]).find(".price_column").val();
		var qty = $(rows[index]).find(".qty_column").val();

		if (!isInt(qty)) {
			alert("Quantity has to be an Integer " + qty);
			$(rows).find(".qty_column").removeClass("error_class");
			$(rows).find(".price_column").removeClass("error_class");
			$(rows[index]).find(".qty_column").addClass("error_class", 1000);
			return false;
		}
		if (!isInt(price * 100)) {
			alert("Price has to be a number");
			$(rows).find(".qty_column").removeClass("error_class");
			$(rows).find(".price_column").removeClass("error_class");
			$(rows[index]).find(".price_column").addClass("error_class", 1000);
			return false;
		}
		if (price <= 0) {
			alert("Price has to be greater than zero");
			$(rows).find(".qty_column").removeClass("error_class");
			$(rows).find(".price_column").removeClass("error_class");
			$(rows[index]).find(".price_column").addClass("error_class", 1000);
			return false;
		}
		if (qty <= 0) {
			alert("Qty has to be greater than zero");
			$(rows).find(".qty_column").removeClass("error_class");
			$(rows).find(".price_column").removeClass("error_class");
			$(rows[index]).find(".qty_column").addClass("error_class", 1000);
			return false;
		}

		var item_code = $(rows[index]).find(".item_code_column").val();
		var store_code = $("[id='purchase_order_form:store_code']").val();

		flag = checkStock(item_code, store_code, qty);
		if (flag == false) {
			$(rows).find(".price_column").removeClass("error_class");
			$(rows).find(".qty_column").removeClass("error_class");
			$(rows[index]).find(".qty_column").addClass("error_class", 1000);
			break;
		}
	}
	return flag;
}

function checkStock(item_code, store_code, poQty) {
	var flag = true;
	var length = $('#stocks_table_body tr').length;
	var rows = stocksTable.fnGetNodes();
	for ( var index = 0; index < length; index++) {
		if ($($(rows[index])).find(".item_code_column").val() == item_code
				&& $($(rows[index])).find(".store_code_column").val() == store_code) {

			var qty = parseInt($($(rows[index])).find(".qty_column").val());
			var hold_qty = parseInt($($(rows[index])).find(".hold_qty_column")
					.val());
			// alert(poQty + " " + qty + " " + hold_qty);
			if (parseInt(poQty) > (qty + hold_qty)) {
				alert(item_code + " in Store: " + store_code
						+ " not enough quantity!");
				flag = false;
			}
		}
	}

	return flag;
}

function isInt(n) {
	return n % 1 == 0;
}
